import {
  listRelationship,
  getRelationship,
  delRelationship,
  addRelationship,
  updateRelationship,
} from "@/apis/user/Relationship";
import { getCompanyList } from "@/apis/com/company";
import { getCookiesUserInfo } from "@/utils/auth";
import moment from "moment";

export const api = {
  list: async (queryParams) => {
    queryParams.companyFrom = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await listRelationship(queryParams);
    return res;
  },
  one: getRelationship,
  delete: delRelationship,
  add: async (queryParams) => {
    queryParams.companyFrom = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addRelationship(queryParams);
    return res;
  },
  update: updateRelationship,
  download: (queryParams) => {
    this.download(
      "company/Relationship/export",
      {
        ...queryParams,
      },
      `Relationship_${new Date().getTime()}.xlsx`
    );
  },
  remote: async (value, remote) => {
    if (remote == "company") {
      let res = await getCompanyList({ name: value });
      if (res.rows) {
        return res.rows.map((item) => {
          return {
            value: item.id,
            label: item.name,
          };
        });
      } else {
        return [];
      }
    }
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "关系企业",
    dataIndex: "companyToName",
    dataType: "Text",
    // remote: "Company",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "关系类型",
    dataIndex: "type",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "相同法人",
      },
      {
        value: 1,
        label: "合作伙伴",
      },
      {
        value: 2,
        label: "关联公司",
      },
      {
        value: 3,
        label: "供应商",
      },
      {
        value: 4,
        label: "竞争对手",
      },
      {
        value: 5,
        label: "最终客户",
      },
    ],
  },
  {
    title: "关系描述",
    dataIndex: "description",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "开始时间",
    dataIndex: "startDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "结束时间",
    dataIndex: "endDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      if (item.remote) {
        newItem.component = "Select-R";
        newItem.componentProps = {
          remote: "company",
        };
      } else {
        newItem.component = "Select";
        newItem.componentProps = {
          options: item.options,
        };
      }
    } else if (item.dataType == "Date") {
      newItem.component = "Range-picker";
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
    };
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        if (item.remote) {
          return record[item.dataIndex + "Name"]
            ? record[item.dataIndex + "Name"]
            : record[item.dataIndex];
        } else {
          let option = item.options.find((option) => {
            return option.value == record[item.dataIndex];
          });
          return option ? option.label : "-";
        }
      };
    } else if (item.dataType == "Date") {
      newItem.customRender = (text, record, index) => {
        let res = moment(record[item.dataIndex]).format("YYYY-MM-DD");
        return res == "Invalid date" ? "-" : res;
      };
    } else {
      newItem.customRender = (text, record, index) => {
        return record[item.dataIndex] ? record[item.dataIndex] : "-";
      };
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    width: 100,
    scopedSlots: { customRender: "action" },
  });
  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      if (item.remote) {
        newItem.component = "Select-R";
      } else {
        newItem.component = "Select";
        newItem.componentProps = {
          options: item.options,
        };
      }
    } else if (item.dataType == "Date") {
      newItem.component = "Date-picker";
    } else if (item.dataType == "File") {
      newItem.component = "Upload";
    }
    return newItem;
  });
  return items;
};

export const loadRemoteOptions = (tableData) => {
  return tableData.map((item) => {
    return {
      label: item.companyToName,
      value: item.companyTo,
    };
  });
};

export const rules = {
  companyTo: [{ required: true, message: "必须选择一个企业" }],
  type: [{ required: true, message: "必须选择一个类型" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 6 },
    wrapperCol: { span: 12 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
